/**
 * 
 */
package com.icbc.cc.api;

import java.io.File;
import java.io.IOException;

import javax.imageio.stream.FileImageOutputStream;

import com.alibaba.fastjson.JSONObject;
import com.icbc.cte.base.CTEConstance;
import com.icbc.cte.base.TranFailException;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.impl.OperationStep;

/**
 * <pre>
 * 身份认证申请
 * <pre>
 * modify by lenovo on 2016-9-12
 * </pre> 
 */
@Input(name = { "orderId", "picType", "picList"}, type = {
		"java:java.lang.String", "java:java.lang.String", "java:java.lang.String" },
		mustInput = { true, true, true }, isVariable = { true, true, true},
		isArray = { false, false, false}, description = { "订单ID", "照片类型: 打款凭条  合同", "照片列表"})
//@Input(name = { "param"}, type = {
//		"java:java.lang.String"},
//		mustInput = { true}, isVariable = { true},
//		isArray = { false}, description = { "传入参数"})
//@Output(name = { "retCode", "retMsg" }, type = { "java:java.lang.String", "java:java.lang.String" }, 
//		isArray = { false, false }, description = { "0:提交成功 其他失败", "" })
public class ImageUpload extends OperationStep {

	public static Log log = LogFactory.getCtpLog(ImageUpload.class);
	
	/* (non-Javadoc)
	 * @see com.icbc.ctp.core.impl.OperationStep#execute(com.icbc.ctp.core.IContext)
	 */
	@Override
	protected int execute(IContext context) throws TranFailException {
		
		String picList = (String)getInputValue(context, "picList");
		String orderId = (String)getInputValue(context, "orderId");
		String picType = (String)getInputValue(context, "picType");
		context.remove("picType");
		context.remove("picList");
		context.remove("osFlag");

		JSONObject inparam=new JSONObject();
		try {
			//将照片存在服务器并获取存储路径
			String path = createFile(orderId,picType,picList);
			
			inparam.put("front", path == null ? "" : path);
			
			AppCommonUtils.excuteOp(inparam.toString(), "add_auth_info");

			setOutputValue(context, "retCode", "0");
			setOutputValue(context, "retMsg", "");
		} catch (Exception e) {
			log.error(e);
			setOutputValue(context, "retCode", "1");
			setOutputValue(context, "retMsg", "请求失败");
		}

		return 0;
	}

	/**
	 * 
	 * 图片保存 
	 * @param fileData
	 * @param suffix
	 * @throws Exception 
	 */
	private String createFile(String orderId, String picType, String picList) throws Exception {


		String webRoot = CTEConstance.getEnvProperty("WEB_ROOT");
		String savePath = CTEConstance.getEnvProperty("auth_save_path");
		String http_path = CTEConstance.getEnvProperty("http_path");
		
		String path="";
		String pathtmp = null;
		FileImageOutputStream out = null;
		try {
			String[] pic=picList.split(",");
			for(int i=0;i<pic.length;i++){				
				byte[] b =pic[i].getBytes();
				String ext = AppCommonUtils.getImageExtension(b);
				String fileName = orderId + "_" + picType+"_"+i+ ext;
				out = new FileImageOutputStream(new File(webRoot + savePath, fileName));
				out.write(b, 0, b.length);
				pathtmp = http_path + savePath + fileName;
				path=path+","+pathtmp;
			}	
			
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return path;
	}
	
}
